package com.work.ioc.iocUtils;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: spring-work
 * @description
 * @author: luffy
 * @create: 2020-07-08 21:54
 */
public class IocUtils {
    private static List<String> classNameList = new ArrayList<>();
    public static List<String> getClassNameList(String packageName) {
        componentScan(packageName);
        return classNameList;

    }
    private static void componentScan(String packageName) {
        URI uri = null;
        try {
            uri = new IocUtils().getClass().getClassLoader().getResource(packageName).toURI();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        String path = uri.getPath();

        File packages = new File(path);

        File[] files = packages.listFiles();

        getClassName(files);

    }

    private static void getClassName(File[] files) {
        for (File file : files) {
            if (file.isDirectory()) {
                File[] files1 = file.listFiles();
                for (File file1 : files1) {
                    getClassName(files1);
                }
            }else {
                String fileName = file.getPath();
                fileName = fileName.substring(64, fileName.length());
                if (fileName.length() > 6 && fileName.substring(fileName.length() - 6, fileName.length()).equals(".class")) {
                    classNameList.add(fileName.substring(0,fileName.length()-6).replace("/","."));
                }

            }
        }
    }



}
